const arr = [1, 2, 3, [1, 2, 3], [[45, 4, 6], [9, 7]]]

console.log(arr);
arr.flat(Infinity)
console.log(arr.flat(Infinity));
console.log(arr);

function flat (arr) {
  if (!Array.isArray(arr)) return [arr]
  let res = []
  for (const val of arr) {
    res = res.concat(flat(val))
  }
  return res
}

let aa = flat(arr)
console.log(aa, 111);